<?php 
namespace xfyzf\plugins\alipayjk\controller;
use xfyzf\api\controller\Examine;
use auth;
use lib\PluginScanner;
class Index{
        public $info = [
                "name"        =>'alipayjk',//支付插件英文名称，需和目录名称一致，不能有重复
                "showname"    =>'支付宝PC挂机',//支付插件显示名称
                "author"      =>'afei66', //支付插件作者
        		'auth'        =>true,//授权开关
        		'pwd'         =>'xfalipaypcgua1745387748####****',//明文 密码
        		'path'        =>'alipaypccode.ini',//授权效验文件  注意 必须唯一 结尾.ini
            		
        	];
public $priKey = 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDUgnkNiOwqIImT
qLogchWeZb/UySZyl+S4hT5vL+63BmJur2SXXbu1HU7SaJERDrmjRubUsT53Lh2H
eNwEYuDDcDJ68E1RwCAwgJV0EQgxpb7j9p2m0OZy7BTQWWQ1MQMmuWU0yN0o+VGN
/QubhrLrjU9CXNgQRTNX7Am6zn7hXjAZDFiF0yUbeczBfnRkaT0w35AtT6tjhXax
dPs1vBpD93hmCSOEZoS0uT0ZFcqvqVv9s/SgdQSwD+quGrBayNBLIIf7L/wWlygC
v+vvEoBUqX2dwOLxAUQdSCKqarkHlaEGs6mB/nGZvEtxDADptBCQlpclQSDf3RwS
nF2wZDcpAgMBAAECggEAOCOTmRi/0s0vqvta2DFrNqIIkEznRESOHpTMjpDqDh24
qSbaewY/DTSF2x3WnNv3/INBfZSpPNDI6j3qWZpJaDfejg2nT15i4o1FbhkFwh/Y
Et7WfPgYuOlXAkdrKoYdIADAFLavfiCHA+yCQX7PC10UQCLIzVuKmcJ4O4pLiHtf
AuLtGGEskN3wri3e6MTVgqpHpjhy+00C1kZaCXfpM1j2eFrBhUsaRlVloJm0zMKb
goWsHr0isqo7wrtCCmmcoEHWr8VEbzU1eY4A3tWhUiIgHKT4Jz5jAFK8GrBsviJL
LPMAxkbelunSwkz3Wj1k9Ug+WPQiiJ3KhY2qQZ5bhQKBgQDyoT9bg1cs2RigATsc
1c/qdfHf/O0Phu+9KWi1tu71FTgQhWFWGfx7BsQhrrc+tFPKYBdEvCOBuYI2vWN8
RE+xJdpAUO+ksulqmMYxsCvhm4KPlvuS0SUr8m/NQWfJBjr+G1ju8Oe99dvPWI3b
bez7LHr50fVbnLBs4c9bY8qAiwKBgQDgOFKoxBxJYLqcV/NNDJHOtVALPMKV5D6S
E52ap2QqWqWILnlQtHiQipuLnbfaEBLkY5Y7CtGhfEyTlnkPESt91z0oTvl3XZ8X
UeGPWiy4i0rT9gvCXMW/jkJVEFflEdr26xXqh7+zERHVGCCNceOiLcds3em6dQPq
a6nVo7qJmwKBgQC82w456S291u33/X1JoJPJ6CxxMKpFX77T+ptqdg3h7rZJBckS
AAAqssWPgjNYN3zRFaKG+fqZSxA0E46Qd4ugU9piHZ2rESfmtSvwGTTO0wD5S4he
azUH1Bl6JuLl8leQ2zCKLDH2Bp0l88PXfOTtTV4bfnYA7SHWXp3MgQxqqQKBgGux
AZqHXqKrr9VptLTcBagHk6NiEn+VuavFehwOq+Bf20N2NtRZadLaj+zV2rkEoV5K
qpw/DQKHxevZCNoAwCfX2kri65S2RbQYosKMaBBW7qCxe2Jl1qCzfg6HJAAkvEt+
C++yNZOW7j6wMOpeuhtp62/RgfIJQ9Ey1saCthRjAoGBAODmx7BtsoBpyd9C7vzC
nxFgodX5/OELmEpPbp9KXgeRTYHLsZFbFQBZWUcJBXdKp8H9JZ825xWhCqV40fQ0
Jtr9Nh+hArtw7bt1N00sbMbLzWU3Yw1DJSJGwsJgmlfzWYPWXg1bZRPpbjNNe1fS
AI8z3/L/pGIQMUy6RUm2Fi/8';//私钥
   
    public function pc_cron(){
        $postdata = input("post");
        $token    = $postdata['token'];
        $cookie   = $postdata['cookie'];
        $uid      = $postdata['uid'];
        $money    = $postdata['money'];
        Examine::checkRequiredParams(array($token,$cookie,$uid,$money));
        $authtoken = Examine::usertokendecode($token);
        if(!$authtoken)exitjson(-1,"token错误");
        $pid = $authtoken['pid'];
        $QR_row = $this->QR_row($uid,$pid);
        if(!$QR_row)exitjson(-1,'二维码不存在');
        $timess = time() +  60;
        if($QR_row['crontime']<time() + mt_rand(3, 5) ){
            $sql = "update `pay_qrlist` set `money`='{$money}',`cookie`='{$cookie}',`crontime`='{$timess}' WHERE `id`='{$QR_row['id']}'";
            if(executeSQLQuery($sql,'update'))exitjson(200,'更新成功');
            exitjson(-1,'更新失败！');
        }
        exitjson(200,'cron ok');
    }
    public function pc_upqr(){
        global  $date;
        $postdata = input("post");
        $token    = $postdata['token'];
        $cookie   = $postdata['cookie'];
        $uid      = $postdata['uid'];
        $money    = $postdata['money'];
        $status   = $postdata['status'];
        Examine::checkRequiredParams(array($token,$cookie,$uid));
        $authtoken = Examine::usertokendecode($token);
        if(!$authtoken)exitjson(-1,"token错误");
        $pid = $authtoken['pid'];
        $QR_row = $this->QR_row($uid,$pid);
        $timess = time() +  60;
        if($QR_row){
            $sql = "update `pay_qrlist` set `cookie`='{$cookie}',`crontime`='{$timess}',`status` = {$status} WHERE `id`='{$QR_row['id']}'";
            if(executeSQLQuery($sql, 'update'))exitjson(200,'更新二维码成功');
            exitjson(500,'更新二维码成功');
        }else{
            $sql = "INSERT INTO `pay_qrlist` (`pid`,`type`, `qr_url`,`cookie`,`money`,`beizhu`,`status`,`hook_type`,`crontime`,`uin`,`addtime`) VALUES (:pid,:type,:qr_url,:cookie,:money,:beizhu,:status,:hook_type,:crontime,:uin,:date)";
            $params = [
                'pid'=>$pid,
                'type'=>'alipay',
                'qr_url'=>"无",
                'cookie'=>$cookie,
                'money'=>"0.00",
                'beizhu'=>$uid,
                'status'=>$status,
                'hook_type'=>'1',
                'crontime'=>$timess,
                'uin'=>$uid,
                'date'=>$date
            ];
            if (safeSqlQuery($sql, $params, 'insert'))exitjson(200,'添加二维码成功！');
            exitjson(-1,'添加失败！'); 
        }
        
    }
    public function pc_notify(){
        $postdata  = input("post");
        $token     = $postdata['token'];
        $money     = $postdata['money'];
        $uid       = $postdata['uid'];
        Examine::checkRequiredParams(array($token,$money,$uid));
        $authtoken = Examine::usertokendecode($token);
        if(!$authtoken)exitjson(-1,"token错误");
        $pid = $authtoken['pid'];
        $QR_row = $this->QR_row($uid,$pid);
        if(!$QR_row)exitjson(-1,'二维码不存在！');
        $sql = "SELECT * FROM pay_order WHERE status=:status  and type=:type and price=:money and outtime>:time and pid = :pid and qr_id = :qr_id  limit 1";
        $paramsSelect = [
            'pid' => $pid,
            'type'=> "alipay",
            'money' => $money,
            'time'=>time(),
            'qr_id' => $QR_row['id'],
            'status'=>0
        ];
        $srow = safeSqlQuery($sql, $paramsSelect)['result'][0]??'';
        if($srow){
            if($srow['status'] == '0'){
                $E_type = "支付宝PC挂机";
                $url=creat_callback($srow);
                $data=get_curl($url['notify']);
                xfhtemail($pid,$money,$E_type,$srow['name'],$srow['trade_no']); 
                createLog($pid,'自动回调订单('.$E_type.')：'.$srow['trade_no'],http_ip());
                $arr = array(
                    'trade_no'=>$srow['trade_no'],
                    'money'=>$money,
                    'qr_id'=>$res['id']
                );
                Examine::exitWithMessage(200,'success',$arr);
            }
        }else{
            exitjson(-1,'无匹配金额');
        }
        
    }
    public function auth(){
        if(PluginScanner::key($this->priKey,$this->info['pwd'],$this->info['path'],$this->info['auth']))exit("success");
    }
    public function Token_effect(){
        $token  = input("post")['token'];
        if(empty($token))exitjson(-1,'请输入token');
        $key =  PluginScanner::key($this->priKey,$this->info['pwd'],$this->info['path'],$this->info['auth']);
        if(auth::mactokenxy($token,pluginpwd,privateKey)){
            file_put_contents(authfile_path, $token);
            exitjson(200,'效验成功！');
        }
         exitjson(-1,'效验失败');
    }
    private function QR_row($uid,$pid){
        $sql = "SELECT * FROM pay_qrlist WHERE  `uin` = :uid and `type`='alipay' and `pid`=:pid and `hook_type` = '1' LIMIT 1";
        $paramsSelect = [
            'uid' => $uid,
            'pid' => $pid
        ];
        
        return safeSqlQuery($sql, $paramsSelect)['result'][0]??'';
    }
}



?>